/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import org.apache.log4j.Logger;


import java.io.Serializable;

import java.util.ArrayList;
import java.util.List;


/**
 * Class corresponding to the "export" element of the exports_conf.xml file.
 * This defines an export configuration object.
 */
public class JBExport implements Serializable
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBExport.class);

    /** Unique name of the export. */
    private String name = null;

    /** Separator of fields. */
    private String separator = null;

    /** Indicator of the headline addition. */
    private String header = null;

    /** Representation mode. */
    private String mode = null;

    /** Format of the attributes list (deprecated). */
    private String lineContent = null;

    /** Quoter. */
    private JBQuotes quoter = new JBQuotes();

    /** List of the elements (JBAttibute or JBValueElement). */
    private List elementsList = new ArrayList();

    /** List of the attribute names. */
    private List attributeNamesList = new ArrayList();

    /** Header presence. */
    private static final String HEADER_PRESENCE = "true";


    /**
     * Gets the export name.
     *
     * @return The name.
     */
    public String getName()
    {
        return name;
    }

    /**
     * Sets the name of the export.
     *
     * @param aname  The name to set.
     */
    public void setName(String aname)
    {
        this.name = aname;
    }

    /**
     * Gets the separator.
     *
     * @return The separator.
     */
    public String getSeparator()
    {
        return separator;
    }

    /**
     * Sets the separator.
     *
     * @param aseparator  The separator to set.
     */
    public void setSeparator(String aseparator)
    {
        this.separator = aseparator;
    }

    /**
     * Gets the header value.
     *
     * @return The header value.
     */
    public String getHeader()
    {
        return header;
    }

    /**
     * Indicates if the export has a header or not.
     *
     * @return true if the header is defined, false otherwise.
     */
    public boolean hasHeader()
    {
        if (header != null)
        {
            return (header.toLowerCase().equals(HEADER_PRESENCE));
        }
        return false;
    }

    /**
     * Sets the header value.
     *
     * @param aheader  The header value to set.
     */
    public void setHeader(String aheader)
    {
        this.header = aheader;
    }

    /**
     * Gets the lineContentValue.
     *
     * @return The lineContent value.
     */
    public String getLineContent()
    {
        return lineContent;
    }

    /**
     * Sets the lineContent value.
     *
     * @param aline  The lineContent value to set.
     */
    public void setLineContent(String aline)
    {
        this.lineContent = aline;
    }

    /**
     * Gets the mode.
     *
     * @return The mode.
     */
    public String getMode()
    {
        return mode;
    }

    /**
     * Indicates if the export mode is line.
     *
     * @return true if the mode is line, false otherwise
     */
    public boolean isLineMode()
    {
        if (mode != null)
        {
            return ((mode.toLowerCase()).equals(JBAttribute.MODE_LINES));
        }
        return false;
    }

    /**
     * Indicates if the export mode is column.
     *
     * @return true if the mode is column, false otherwise
     */
    public boolean isColumnMode()
    {
        if (mode != null)
        {
            return ((mode.toLowerCase()).equals(JBAttribute.MODE_COLS));
        }
        return false;
    }

    /**
     * Sets the mode.
     *
     * @param amode  The mode to set.
     */
    public void setMode(String amode)
    {
        this.mode = amode;
    }

    /**
     * Gets the quoter.
     *
     * @return The quoter object.
     */
    public JBQuotes getQuoter()
    {
        return quoter;
    }

    /**
     * Sets the quoter.
     *
     * @param aquoter  The quoter to set.
     */
    public void setQuoter(JBQuotes aquoter)
    {
        this.quoter = aquoter;
    }

    /**
     * Gets the attributes names list of the export.
     *
     * @return The attributes names list.
     */
    public List getAttributesList()
    {
        return attributeNamesList;
    }

    /**
     * Adds an element to the elments list of the export.
     *
     * @param aobj  The element to add.
     */
    public void addElementsList(Object aobj)
    {
        elementsList.add(aobj);
        if (aobj instanceof JBAttribute)
        {
            attributeNamesList.add(((JBAttribute) aobj).getValue());
        }
    }

    /**
     * Finds an attribute in the list of elements.
     *
     * @param aname  Name of the attribute.
     *
     * @return The attribute.
     */
    public JBAttribute findAttribute(String aname)
    {
        if (!elementsList.isEmpty())
        {
            for (int i = 0; i < elementsList.size(); i++)
            {
                Object obj = elementsList.get(i);
                if (obj instanceof JBAttribute)
                {
                    String attributeName = ((JBAttribute) obj).getValue();
                    if (attributeName.equals(aname))
                    {
                        return (JBAttribute) obj;
                    }
                }
            }
        }
        return null;
    }

    /**
     * Gets the elements list of the export.
     *
     * @return The elements list.
     */
    public List getElementsList()
    {
        return elementsList;
    }

    /**
     * Sets the elements list of the export.
     *
     * @param alist  The list to set.
     */
    public void setElementsList(List alist)
    {
        this.elementsList = alist;
    }

    /**
     * Gets a string representation of the export.
     *
     * @return The string representation.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();

        buf.append("name = ");
        buf.append(name);
        buf.append(System.getProperty("line.separator"));
        buf.append("separator = ");
        buf.append(separator);
        buf.append(System.getProperty("line.separator"));
        buf.append("header = ");
        buf.append(header);
        buf.append(System.getProperty("line.separator"));
        buf.append("mode = ");
        buf.append(mode);
        buf.append(System.getProperty("line.separator"));
        buf.append("lineContent = ");
        buf.append(lineContent);
        buf.append(System.getProperty("line.separator"));

        for (int i = 0; i < elementsList.size(); i++)
        {
            Object obj = elementsList.get(i);
            if (obj instanceof JBValueElement)
            {
                buf.append(((JBValueElement) obj).getValue());
            }
            else if (obj instanceof JBAttribute)
            {
                buf.append(((JBAttribute) obj).getName());
            }
            buf.append(System.getProperty("line.separator"));
        }
        buf.append(System.getProperty("line.separator"));

        return buf.toString();
    }

}
